/*
 * Copyright (C) 2012 Martin Wilsdorf <mwjutils-api@googlecode.com>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

package com.google.mwjutils.xcode;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author Martin Wilsdorf <mwjutils-api@googlecode.com>
 */
public class XLineReader extends XReader implements XLineInput {
    public XLineReader( InputStream in ) {
        super(in);
        fIStreamReader  = new InputStreamReader( in );
        fBufferedReader = new BufferedReader( fIStreamReader );
    }
    
    public XLineReader(  ) {
        super( System.in );
        fIStreamReader  = new InputStreamReader( System.in );
        fBufferedReader = new BufferedReader( fIStreamReader );
    }
    
    @Override
    public final int read() throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    @Override
    public String readLine(  ) throws FileNotFoundException, IOException {
        String line;
        if ( ( line = fBufferedReader.readLine(  ) ) != null ) {
            return line;
        } else return null;
    }
    
    @Override
    public String[] readLines(  ) throws FileNotFoundException, IOException {
        List<String> lines = new ArrayList<>(  );
        for ( String line; ( line = fBufferedReader.readLine(  ) ) != null; ) {
            lines.add( line );
        }
        return lines.toArray(new String[lines.size()]);
    }
    
    private BufferedReader    fBufferedReader;
    private InputStreamReader fIStreamReader;
}
